package com.me.mygdxgame.chatbox;

import kryonetpackets.MessagePacket;
import client.GameClient;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;


import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;


import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.ui.Window;

public class ChatBox extends Window {
	private static Skin fieldSkin;
	private final Label text;
	private final TextField input;
	private GameClient gameClient;
	private ScrollPane textArea;
	

	
	public ChatBox(Skin skin, GameClient gameClient)
	{
		
		super("Chat Box", skin);
		this.fieldSkin = skin;
		this.gameClient = gameClient;
		
		this.setBounds(0, 0, 400, 200);
		this.setColor(new Color(1,1,1,0.5f));
		
		TextButton button = new TextButton("Send", fieldSkin);
		button.setPosition(310, 5);
		button.setSize(85, 25);
		
		 input = new TextField("", skin);
		input.setSize(300, 25);
		input.setPosition(5, 5);
		
		text = new Label("",skin);
		text.setWrap(true);
		text.setSize(1000, 1000);
		//text.setFillParent(true);
	
		
		textArea = new ScrollPane(text,skin);
	    textArea.setPosition(5, 40);
		textArea.setSize(385, 130);
		textArea.setScrollbarsOnTop(true);
		textArea.setFadeScrollBars(false);
	
		
		
		
		
		this.addActor(button);
		this.addActor(input);
		this.addActor(textArea);
		//this.debug();
		
		button.addListener( new InputListener(){
			public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
				if (input.getText() != " ") {
					sendMessage();
					input.setText(" ");
					
					return true;
				}
				return false;
			}
			
		});
		this.addListener( new InputListener(){
			public boolean keyDown (InputEvent Event, int keyCode)
			{
				if (keyCode == Keys.ENTER)
				{
					sendMessage();
					input.setText(" ");
				}
				return false;
			}
		});
			
		
	}
	public void appendText(String input)
	{
		//TODO: Fix scrolling 
		text.setText(text.getText() + "\n" + input);
		textArea.setScrollPercentY(textArea.getScrollPercentY() + 100);
		
	}
	public void clearText()
	{
		text.setText(" ");
	}
	public void sendMessage()
	{
		//gameClient.getClass();
		gameClient.sendPacket(new MessagePacket(this.gameClient.getThisPlayerName(),this.input.getText()));
	}
}

